﻿using System.Net.Mail;
using System.Data;
using System;

    public class MailHelper
    {
        public static void DySend(string title, string content, string mailto)
        {
            MailMessage msg = new MailMessage();
            msg.To.Add(mailto);
            string sqlemail = "select email,sender,emailpass,smtp from admins where id=1";
            DataRow dr = DBFactory.GetConn().exeRow(sqlemail);
            msg.From = new MailAddress(dr["email"].ToString(), dr["sender"].ToString(), System.Text.Encoding.UTF8);
            msg.Subject = title;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = content;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(dr["email"].ToString(), dr["emailpass"].ToString());

            client.Host = dr["smtp"].ToString();
            object userState = msg;
            {
                client.Send(msg);
            }
        }
        public static void Send(string title, string content, string mailto)
        {
            MailMessage msg = new MailMessage();
            msg.To.Add(mailto);
            msg.From = new MailAddress("cpengf@163.com", "螃蟹秘密", System.Text.Encoding.UTF8);
            //msg.From = new MailAddress("gball@126.com", "功夫篮球管理小组", System.Text.Encoding.UTF8);
            //msg.From = new MailAddress("czcz1024@gmail.com", "czcz", System.Text.Encoding.UTF8);
            msg.Subject = title;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = content;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;
            
            SmtpClient client = new SmtpClient();
            //client.Credentials = new System.Net.NetworkCredential("czcz1024@gmail.com", "801024");
            client.Credentials = new System.Net.NetworkCredential("cpengf@163.com", "pengfei1983");
            //使用gmail
            //client.Port = 587;
            //client.Host = "smtp.gmail.com";
            //client.EnableSsl = true;
            
            client.Host = "smtp.163.com";
            object userState = msg;
            try
            {
                //client.SendAsync(msg, userState);
                client.Send(msg);
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message);
            }            
        }
    }

